package cn.djb.day21;

public class Solution {
    // you need to treat n as an unsigned value

    /**
     * 使用 n&1
     *
     * @param n
     * @return
     */
    public int hammingWeight(int n) {
        int res = 0;
        while (n != 0) {
            res += n & 1;
            n >>>= 1;
        }
        return res;
    }


    /**
     * 使用 n&(n−1)
     *
     * @param n
     * @return
     */
    public int hammingWeight2(int n) {
        int res = 0;
        while (n != 0) {
            res++;
            n = n & (n - 1);
        }
        return res;
    }


}